package com.meisupic.copyright.amdin.controller;

import com.meisupic.copyright.domain.po.SysDicPo;
import com.meisupic.copyright.domain.query.SysDicQuery;
import com.meisupic.copyright.domain.vo.SysDicVo;
import com.meisupic.copyright.service.ISysDicService;
import com.meisupic.copyright.support.Message;
import com.meisupic.copyright.support.PageResponse;
import com.meisupic.copyright.util.BeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * creation wuxun
 * @Date 2018/1/22 上午11:19
 */
@RestController
@RequestMapping("/admin/sysDic")
public class SysDicAdminController {

  @Autowired
  private ISysDicService sysDicService;

  @GetMapping(value = "/page")
  public PageResponse<SysDicVo> queryPage(SysDicQuery sysDicQuery,
      @PageableDefault(sort = {"type","sortNo","createTime"}) Pageable pageable) {
    PageRequest pageRequest = new PageRequest(pageable.getPageNumber()-1,pageable.getPageSize(),pageable.getSort());
    Page<SysDicVo> sysDicPoPage = sysDicService.findPage(sysDicQuery, pageRequest);
    PageResponse<SysDicVo> pageResponse = new PageResponse<>(sysDicPoPage.getTotalElements(),sysDicPoPage.getContent());
    return pageResponse;
  }

  @PostMapping(value = "/save")
  public Message saveSysDic(@RequestBody SysDicVo sysDicVo) {
    SysDicPo sysDicPo = BeanMapper.map(sysDicVo, SysDicPo.class);
    sysDicService.save(sysDicPo);
    return Message.success(true);
  }

  @GetMapping("findById")
  public Message<SysDicVo> findSysDicById(Long id) {
    SysDicVo sysDicVo = sysDicService.findById(id);
    return Message.success(sysDicVo);
  }

}
